//
//  MyStockService.m
//  Stock
//
//  Created by blacksun on 11-6-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyStockService.h"
#import "NSString+SBJSON.h"


@implementation MyStockService

-(void)addMyStockWithIds:(NSString *)ids{
    NSString *queryUrlStr = [[NSString alloc] initWithFormat:@"%@stock/watch?u=%@&id=%@",kHostName,[StockContext shareInstance].userId,ids];
    NSLog(@"%@",queryUrlStr);
    NSURL *url = [[NSURL alloc] initWithString:queryUrlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
    [queryUrlStr release];
}
-(void)deleteMyStockWithIds:(NSString *)ids{
    NSString *queryUrlStr = [[NSString alloc] initWithFormat:@"%@stock/dismiss?u=%@&id=%@",kHostName,[StockContext shareInstance].userId,ids];
    NSLog(@"%@",queryUrlStr);
    NSURL *url = [[NSURL alloc] initWithString:queryUrlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
    [queryUrlStr release];
}

-(void)queryStockForceTraceWithStockId:(NSString *)stockId{
    NSString *queryUrlStr = [[NSString alloc] initWithFormat:@"%@stock/forcetrack?id=%@",kHostName,stockId];
    NSLog(@"%@",queryUrlStr);
    NSURL *url = [[NSURL alloc] initWithString:queryUrlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kMyStockServiceQueryForceTrace,kASIHttpRequestUserinfoKey,stockId,kStockIdKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
    [queryUrlStr release];
}
-(void)queryStockFundFlowWithStockId:(NSString *)stockId{
    NSString *queryUrlStr = [[NSString alloc] initWithFormat:@"%@stock/fundflow?id=%@",kHostName,stockId];
    NSLog(@"%@",queryUrlStr);
    NSURL *url = [[NSURL alloc] initWithString:queryUrlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kMystockServiceQueryFundFlow,kASIHttpRequestUserinfoKey,stockId,kStockIdKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
    [queryUrlStr release];
}

-(void)queryStockOpinionCenterDetailWithStockId:(NSString *)stockId{

    NSString *queryUrlStr = [[NSString alloc] initWithFormat:@"%@opinion/detail?id=%@",kHostName,stockId];
    NSLog(@"%@",queryUrlStr);
    NSURL *url = [[NSURL alloc] initWithString:queryUrlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kMyStokcServiceQueryOpinionDetail,kASIHttpRequestUserinfoKey,stockId,kStockIdKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
    [queryUrlStr release];
}


-(void)updateMyStocks:(NSArray *)myStocks{
    if (!myStocks || [myStocks count]==0) {
        return;
    }
    NSMutableArray *stockIds = [NSMutableArray array];
    for (MyStock *myStock in myStocks) {
        [stockIds addObject:myStock.uuid];
    }
    NSString *ids = [stockIds componentsJoinedByString:@","];
    NSString *urlStr = [NSString stringWithFormat:@"%@stock/brief?stocks=%@",kHostName,ids];
    NSURL *url = [NSURL URLWithString:urlStr];
    //NSLog(@"%@",urlStr);
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kMyStockServiceUpdateStock,kASIHttpRequestUserinfoKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    
}


-(void)queryStock:(NSString *)stockCode{
    NSString *urlStr = [NSString stringWithFormat:@"%@stock/query?code=%@",kHostName,stockCode];
    NSLog(@"%@",urlStr);
    NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kMyStockServiceQueryStock,kASIHttpRequestUserinfoKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
}

-(void)uploadStockRadar:(StockRadar*)radar{
    int upOrDown = radar.type==kStockMonitorTypeUp ? 0 : 1;
    int notifyType = radar.repeat? 9 : 1;
    NSString *urlStr = [NSString stringWithFormat:@"%@stock/monitor?u=%@&id=%@&d=%d&t=%d&p=%@",kHostName,[StockContext shareInstance].userId,radar.stockId,upOrDown,notifyType,radar.targetValue];
    //NSLog(@"%@",urlStr);
    NSURL *url = [[NSURL alloc] initWithString:urlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kMyStokcServiceUploadStockRadar,kASIHttpRequestUserinfoKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
}



- (void)requestFinished:(ASIHTTPRequest *)request{
    //NSLog(@"request finished");
    NSString *responseString = [request responseString];
    NSString *action = [request.userInfo objectForKey:kASIHttpRequestUserinfoKey];
    if ([kMyStockServiceUpdateStock isEqualToString:action]) {//update mystocks
        NSArray *stockDicts = [[responseString JSONValue] objectForKey:@"stocks"];
        NSMutableArray *myStocks = [NSMutableArray array];
        for (NSDictionary *stockDict in stockDicts) {
            MyStock *myStock = [[MyStock alloc] init];
            [[CommonUtil shareInstance] copyMapEntries:stockDict toObject:myStock];
            [myStocks addObject:myStock];
            [myStock release];
        }
        [StockContext shareInstance].remoteMyStocks = myStocks;
        [[StockContext shareInstance] findRemoteStockWithStockId:@"sz00002"];
        [self.delegate requestFinishedWithResult:myStocks andUserinfo:request.userInfo];
    }else if([kMyStockServiceQueryStock isEqualToString:action]){
        NSArray *stocksDicts = [[responseString JSONValue] objectForKey:@"stocks"];
        NSMutableArray *stocks = [NSMutableArray array];
        for (NSDictionary *dict in stocksDicts) {
            MyStock *myStock = [[MyStock alloc] init];
            [[CommonUtil shareInstance] copyMapEntries:dict toObject:myStock];
            [stocks addObject:myStock];
            [myStock release];
        }
        [self.delegate requestFinishedWithResult:stocks andUserinfo:request.userInfo];
    }else if([kMyStockServiceQueryForceTrace isEqualToString:action]){
        NSDictionary *ditc = [responseString JSONValue];
        NSString *stockId = [request.userInfo objectForKey:kStockIdKey];
        MyStockForceTrace *forceTrace = [[MyStockForceTrace alloc] init];
        [[CommonUtil shareInstance] copyMapEntries:ditc toObject:forceTrace];
        forceTrace.stockId = stockId;
        [self.delegate requestFinishedWithResult:forceTrace andUserinfo:request.userInfo];
        [[MyStockForceTrace findByStockId:stockId] deleteObject];
        [forceTrace save];
        [forceTrace release];
    }else if([kMystockServiceQueryFundFlow isEqualToString:action]){
        NSDictionary *ditc = [responseString JSONValue];
        NSString *stockId = [request.userInfo objectForKey:kStockIdKey];
        MyStockFundFlow *fundFlow = [[MyStockFundFlow alloc] init];
        [[CommonUtil shareInstance] copyMapEntries:ditc toObject:fundFlow];
        fundFlow.stockId = stockId; 
        [self.delegate requestFinishedWithResult:fundFlow andUserinfo:request.userInfo];
        [[MyStockFundFlow findByStockId:stockId] deleteObject];
        [fundFlow save];
        [fundFlow release];
    }else if([kMyStokcServiceUploadStockRadar isEqualToString:action]){
        [self.delegate requestFinishedWithResult:nil andUserinfo:request.userInfo];
    }else if([kMyStokcServiceQueryOpinionDetail isEqualToString:action]){
        [self.delegate requestFinishedWithResult:[responseString JSONValue] andUserinfo:request.userInfo];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    [self.delegate requestFinishedwithError:request.error andUserinfo:request.userInfo];
}
    
@end
